﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using AliPayApi.Core;

namespace AliPayApi.Api.Direct_Pay
{
    public class SendInfo
    {
        #region 支付属性
        //支付类型
        private string _payment_type = "1";
        /// <summary>
        /// 支付类型 只读
        /// </summary>
        public string Payment_type
        {
            get { return _payment_type; }
        }

        //服务器异步通知页面路径 
        private string platform_Notify_Url;
        /// <summary>
        /// 服务器异步通知页面路径，需http://格式的完整路径，不能加?id=123这类自定义参数
        /// </summary>
        public string Platform_Notify_Url
        {
            get { return platform_Notify_Url; }
            set { platform_Notify_Url = value; }
        }

        //页面跳转同步通知页面路径  
        private string platform_Return_Url;
        /// <summary>
        /// 页面跳转同步通知页面路径 , 需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
        /// </summary>
        public string Platform_Return_Url
        {
            get { return platform_Return_Url; }
            set { platform_Return_Url = value; }
        }

        //本支付接口异步通知页面路径
        private string _notify_url;
        /// <summary>
        /// 本支付接口异步通知页面路径，需http://格式的完整路径，不能加?id=123这类自定义参数
        /// </summary>
        public string Notify_url
        {
            get { return _notify_url; }
            set { _notify_url = value; }
        }

        //本支付接口页面跳转同步通知页面路径
        private string _return_url;
        /// <summary>
        /// 本支付接口页面跳转同步通知页面路径 , 需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
        /// </summary>
        public string Return_url
        {
            get { return _return_url; }
            set { _return_url = value; }
        }

        //卖家支付宝帐户
        private string _seller_email;
        /// <summary>
        /// 卖家支付宝帐户,必填
        /// </summary>
        public string Seller_email
        {
            get { return _seller_email; }
            set { _seller_email = value; }
        }

        //商户订单号
        private string _out_trade_no;
        /// <summary>
        /// 商户订单号，商户网站订单系统中唯一订单号，必填
        /// </summary>
        public string Out_trade_no
        {
            get { return _out_trade_no; }
            set { _out_trade_no = value; }
        }

        //订单名称
        private string _subject;
        /// <summary>
        /// 订单名称，必填
        /// </summary>
        public string Subject
        {
            get { return _subject; }
            set { _subject = value; }
        }

        //付款金额
        private string _total_fee;
        //付款金额，必填
        public string Total_fee
        {
            get { return _total_fee; }
            set { _total_fee = value; }
        }

        //订单描述
        private string _body;
        /// <summary>
        /// 订单描述
        /// </summary>
        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }

        //商品展示地址
        private string _show_url;
        /// <summary>
        /// 商品展示地址，需以http://开头的完整路径，例如：http://www.xxx.com/myorder.html
        /// </summary>
        public string Show_url
        {
            get { return _show_url; }
            set { _show_url = value; }
        }

        private string _anti_phishing_key;
        /// <summary>
        /// 防钓鱼时间戳,，若要使用请调用类文件submit中的query_timestamp函数
        /// </summary>
        public string Anti_phishing_key
        {
            get { return _anti_phishing_key; }
            set { _anti_phishing_key = value; }
        }

        //客户端的IP地址
        private string _exter_invoke_ip;
        /// <summary>
        /// 客户端的IP地址， 非局域网的外网IP地址，如：221.0.0.1
        /// </summary>
        public string Exter_invoke_ip
        {
            get { return _exter_invoke_ip; }
            set { _exter_invoke_ip = value; }
        }

        //返回数据的编码格式
        private string _responeCode;
        /// <summary>
        /// 返回数据的编码格式
        /// </summary>
        public string ResponeCode
        {
            get { return _responeCode; }
            set { _responeCode = value; }
        }

        #endregion

        public SendInfo()
        {
            Platform_Notify_Url = Config.GetAppSetting("platform_notify_url");
            platform_Return_Url = Config.GetAppSetting("platform_return_url");
            Notify_url = Config.GetAppSetting("notify_url");
            Return_url = Config.GetAppSetting("return_url");
            Seller_email = "";
            Out_trade_no = "";
            Subject = "";
            Total_fee = "";
            Body = "";
            Show_url = "";
            Anti_phishing_key = "";
            Exter_invoke_ip = "";
            ResponeCode = "utf-8";
        }

        private bool CheckParamsBeforeSend()
        {
            var checkList = new List<string>();
            var checkListName = new List<string>();

            checkList.Add(Payment_type);
            checkListName.Add("Payment_type");

            checkList.Add(Platform_Notify_Url);
            checkListName.Add("Platform_Notify_Url");

            checkList.Add(Notify_url);
            checkListName.Add("Notify_url");

            checkList.Add(Platform_Return_Url);
            checkListName.Add("Platform_Return_Url");

            checkList.Add(Return_url);
            checkListName.Add("Return_url");

            checkList.Add(Seller_email);
            checkListName.Add("Seller_email");

            checkList.Add(Out_trade_no);
            checkListName.Add("Out_trade_no");

            checkList.Add(Subject);
            checkListName.Add("Subject");

            checkList.Add(Total_fee);
            checkListName.Add("Total_fee");

            //checkList.Add(Body);
            //checkListName.Add("Body");

            //checkList.Add(Show_url);
            //checkListName.Add("Show_url");

            //checkList.Add(Anti_phishing_key);
            //checkListName.Add("Anti_phishing_key");

            //checkList.Add(Exter_invoke_ip);
            //checkListName.Add("Exter_invoke_ip");

            var allowChek = true;
            var index = 0;
            foreach (var tmp in checkList)
            {
                if (string.IsNullOrEmpty(tmp))
                {
                    allowChek = false;
                    throw new Exception(string.Format("{0}为必填项", checkListName[index]));
                }
                index += 1;
            }
            return allowChek;
        }

        /// <summary>
        /// form提交到对应网址
        /// </summary>
        /// <returns></returns>
        public string SendForm()
        {
            if (!CheckParamsBeforeSend())
            {
                return null;
            }

            //把请求参数打包成数组
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
            sParaTemp.Add("partner", Config.Partner);
            sParaTemp.Add("_input_charset", Config.Input_charset.ToLower());
            sParaTemp.Add("service", "create_direct_pay_by_user");
            sParaTemp.Add("payment_type", Payment_type);
            sParaTemp.Add("notify_url", Platform_Notify_Url);
            sParaTemp.Add("return_url", Platform_Return_Url);
            sParaTemp.Add("seller_email", Seller_email);
            sParaTemp.Add("out_trade_no", Out_trade_no);
            sParaTemp.Add("subject", Subject);
            sParaTemp.Add("total_fee", Total_fee);
            sParaTemp.Add("body", Body);
            sParaTemp.Add("show_url", Show_url);
            sParaTemp.Add("anti_phishing_key", Anti_phishing_key);
            sParaTemp.Add("exter_invoke_ip", Exter_invoke_ip);

            //建立请求
            return Submit.BuildRequest(sParaTemp, "get", "提交");
        }

        /// <summary>
        /// 模拟Post提交，不改变当前网址
        /// </summary>
        /// <returns></returns>
        public string SendBySimulationPost()
        {
            if (!CheckParamsBeforeSend())
            {
                return null;
            }

            //把请求参数打包成数组
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
            sParaTemp.Add("partner", Config.Partner);
            sParaTemp.Add("_input_charset", Config.Input_charset.ToLower());
            sParaTemp.Add("service", "create_direct_pay_by_user");
            sParaTemp.Add("payment_type", Payment_type);
            sParaTemp.Add("notify_url", Platform_Notify_Url);
            sParaTemp.Add("return_url", Platform_Return_Url);
            sParaTemp.Add("seller_email", Seller_email);
            sParaTemp.Add("out_trade_no", Out_trade_no);
            sParaTemp.Add("subject", Subject);
            sParaTemp.Add("total_fee", Total_fee);
            sParaTemp.Add("body", Body);
            sParaTemp.Add("show_url", Show_url);
            sParaTemp.Add("anti_phishing_key", Anti_phishing_key);
            sParaTemp.Add("exter_invoke_ip", Exter_invoke_ip);

            //建立请求
            return Submit.BuildRequest(sParaTemp, ResponeCode);
        }
    }
}
